<template>
	<div>
		<el-dialog :visible.sync="visible1" title="仓库信息" top="3%" width="30%">
			<el-form ref="form" :model="form" :rules="formRules" label-width="100px">
				<el-form-item label="仓库名称" prop="storageName">
					<el-input v-model="form.storageName" style="width: 100%;"></el-input>
				</el-form-item>
				<el-form-item label="仓库编码" prop="storageNum">
					<el-input v-model="form.storageNum" style="width: 100%;"></el-input>
				</el-form-item>
				<el-form-item label="存储类型" prop="storeType">
					<el-select v-model="form.storeType" placeholder="请选择存储类型" style="width: 100%;">
						<el-option :label="item.value" :value="item.code" v-for="(item,index) in storageTypeList" :key="index">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="仓库说明" prop="remark">
					<el-input type="textarea" v-model="form.remark" style="width: 100%;"></el-input>
				</el-form-item>
				<el-form-item label="设计存放量" prop="storageAmount">
					<el-input v-model="form.storageAmount" style="width: 100%;"></el-input>
				</el-form-item>
				<el-form-item label="贮存标识" prop="officialStorageId">
					<el-input v-model="form.officialStorageId" placeholder="贮存标识标识用于省系统API对接，请勿随意填写" style="width: 100%;">
					</el-input>
				</el-form-item>
				<el-form-item label="计量单位">
					<el-select v-model="form.unitCode" placeholder="请选择计量单位" style="width: 100%;">
						<el-option :label="item.value" :value="item.code" v-for="(item,index) in unitList"></el-option>
					</el-select>
				</el-form-item>
				
			</el-form>
			
			<div style="text-align: center;" slot="footer">
				<el-button v-if="form && form.id" type="danger" @click="deleteThis">删除</el-button>
				<el-button type="primary" @click="saveOrUpdate">保存</el-button>
				<el-button @click="cancle">取消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	let that
	export default {
		name: 'addStock',
		components: {},
		props: ['entId', 'editData'],
		data() {
			return {
				visible1: false,
				params: {
					facilitatorId: '',
					categoryMainId: '',
					ticketId: '',
					itemId: '',
					subjectId: '',
				},
				form: {
					storageName: '',
					storageNum: '',
					storageAmount: '',
					remark: '',
					unitCode: 'T'
				},
				unitList: [],
				formRules: {
					storageName: [{
						required: true,
						message: '请输入仓库名称',
						trigger: 'blur'
					}],
					storageNum: [{
						required: true,
						message: '请输入仓库编码',
						trigger: 'blur'
					}],
					storageAmount: [{
						pattern: /^[\.\d]*$/,
						message: '设计库存量必须为大于0的数字',
						trigger: 'change'
					}, ],
				},
				storageTypeList: [{
						code: 'COMMON_STORAGE',
						value: '贮存库'
					},
					{
						code: 'PRODUCT_STORAGE',
						value: '成品库'
					},
					{
						code: 'PRODUCE_STORAGE',
						value: '生产区'
					},
					{
						code: 'PREVIEW_STORAGE',
						value: '预处理区'
					}
				],
				storeType: [{
						code: 'TANK',
						value: '储罐'
					},
					{
						code: 'STORE',
						value: '仓库'
					},
					{
						code: 'POOL',
						value: '贮存池'
					}
				],
			}
		},
		created: function() {},
		mounted: function() {
			that = this
			that.params.facilitatorId = that.getQueryString("facilitatorId")
			that.params.categoryMainId = that.getQueryString("categoryMainId")
			that.params.ticketId = that.$store.state.token
			that.params.itemId = that.getQueryString("itemId")
			that.params.subjectId = that.getQueryString("subjectId")

			this.getUnitList();
			
		},
		methods: {
			open(editData) {
				if (editData) {
					that.form = editData
				} else {
					that.form = that.$options.data().form
				}
				that.$forceUpdate()
				that.visible1 = true
			},
			
			cancle() {
				that.visible1 = false
				that.$emit("cancle", true)
			},

			//删除危废
			deleteThis() {
				this.$confirm('此操作将删除此数据, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					that.ajax({
						url: '/hw/storageBase/deleteStorageBase?ticketId=' + that.params.ticketId,
						data: {
							id: that.form.id
						},
						contentType: 'application/x-www-form-urlencoded',
						success: function(result) {
							if (result.status && result.data) {
								that.visible1 = false
								that.$emit("success", true)
							}
						}
					})
				})
			},

			saveOrUpdate: function() {
				var _this = this;
				delete _this.form.createTime
				this.$refs['form'].validate(function(valid) {
					if (valid) {
						_this.form.entId = _this.entId;
						_this.form.storageType = 'COMMON_STORAGE';
						that.ajax({
							url: '/hw/storageBase/saveOrUpdateStorageBase.htm?ticketId=' + that.params
								.ticketId,
							data: JSON.stringify(_this.form),
							contentType: 'application/json',
							success: function(result) {
								if (result.status == 1 && result.data) {
									that.data = that.$options.data()
									that.visible1 = false
									that.$forceUpdate()
									if (_this.form.id) {
										that.$emit("success", true)
									} else {
										that.$emit("success", true)
									}
									that.$message.success("保存成功！")
								} else {}
							}
						});
					}
				});
			},
			getUnitList: function() {
				var _this = this;
				that.ajax({
					url: '/codeType/listCodeValue',
					data: {
						typeCode: 'UNIT_TYPE'
					},
					success: function(data) {
						if (data.status == 1) {
							_this.unitList = data.data;
						}
					}
				});
			},

		}
	}
</script>

<style>

</style>